package com.yt.ds.chapter4;

import com.yt.ds.chapter1.Bag;
import com.yt.ds.io.In;

/**
 * @Author: lzf
 * @Date: 2020/7/30 09:58
 * @Description: 无向图
 */
public class UnDigraph implements Graph{
    /**
     * 顶点数
     */
    private final int vertexNum;
    /**
     * 边数
     */
    private int edgeNum;
    /**
     * 邻接表[存邻接的顶点]
     */
    private Bag<Integer>[] adj;

    public UnDigraph(int v) {
        this.vertexNum = v;
        this.edgeNum = 0;
        adj = (Bag<Integer>[]) new Bag[v];
        for (int i = 0; i < v; i++) {
            adj[i] = new Bag<>();
        }
    }

    public UnDigraph(In in) {
        this(in.readInt());
        int e = in.readInt();
        for (int i = 0; i < e; i++) {
            int v = in.readInt();
            int w = in.readInt();
            addEdge(v, w);
        }
    }

    /**
     * 获取顶点数
     */
    @Override
    public int getVertexNum() {
        return vertexNum;
    }

    /**
     * 获取边数
     */
    @Override
    public int getEdgeNum() {
        return edgeNum;
    }

    /**
     * 描述：添加一条边
     *
     * @param v 顶点下标
     * @param w 顶点下标
     * @return "void"
     * @date: 2020/7/27 16:50
     */
    @Override
    public void addEdge(int v, int w) {
        adj[v].add(w);
        adj[w].add(v);
        edgeNum++;
    }

    /**
     * 描述：获取v相邻的顶点
     *
     * @param v 目标顶点
     * @return "java.lang.Iterable<java.lang.Integer>"
     * @date: 2020/7/27 16:50
     */
    @Override
    public Iterable<Integer> adj(int v) {
        return adj[v];
    }
}
